//
//  UIViewController+Extension.swift
//  DoShare
//
//  Created by 申铭 on 2018/5/14.
//  Copyright © 2018年 eetrust. All rights reserved.
//

import UIKit

extension UIWindow {
    static var key: UIWindow? {
        if #available(iOS 13, *) {
            return UIApplication.shared.windows.filter {$0.isKeyWindow}.first
        } else {
            return UIApplication.shared.keyWindow
        }
    }
}

extension UIViewController {
    func push() {
        FCurrentNav()?.pushViewController(self, animated: true)
    }
    
    // 获取当前ViewController
    class func currentViewController(base: UIViewController? = UIWindow.key?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return currentViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            return currentViewController(base: tab.selectedViewController)
        }
        if let presented = base?.presentedViewController {
            return currentViewController(base: presented)
        }
        return base
    }
    
    class func currentNavController() -> UINavigationController? {
        let vc = UIViewController.currentViewController()
        return vc?.navigationController
    }
}

